package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.client.CourseEsClient;
import cn.itsource.hrm.controller.vo.CourseVo;
import cn.itsource.hrm.doc.CourseDoc;
import cn.itsource.hrm.domain.Course;
import cn.itsource.hrm.domain.CourseDetail;
import cn.itsource.hrm.domain.CourseMarket;
import cn.itsource.hrm.mapper.CourseDetailMapper;
import cn.itsource.hrm.mapper.CourseMapper;
import cn.itsource.hrm.mapper.CourseMarketMapper;
import cn.itsource.hrm.mapper.CourseTypeMapper;
import cn.itsource.hrm.query.CourseQuery;
import cn.itsource.hrm.util.PageList;
import cn.itsource.hrm.service.ICourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jinhui
 * @since 2020-05-25
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseEsClient courseEsClient;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private CourseTypeMapper courseTypeMapper;


    /**
     * 分页高级查询
     * @param query
     * @return
     */
    @Override
    public PageList<Course> queryPage(CourseQuery query) {

        IPage<Course> iPage =
                baseMapper.selectPage(new Page<Course>(query.getPage(), query.getRows()), query);
        PageList<Course> pageList =
                new PageList<>(iPage.getTotal(),iPage.getRecords());
        return pageList;

    }

    /**
     * 修改
     * @param course
     */
    @Override
    public void updateById(CourseVo course) {

    }


    @Override
    public void online(List<Long> ids) {
        //根据id查询出课程信息
        List<Course> coursesList = baseMapper.selectBatchIds(ids);
        System.out.println(coursesList);
        //转换
        List<CourseDoc> coursesToDocs=coursesToDoc(coursesList);
        System.out.println(coursesToDocs);
        //保存到es中
        courseEsClient.saveBatch(coursesToDocs);
        //修改课程状态，上线时间保存到数据库中
        baseMapper.online(ids,System.currentTimeMillis());

    }

    private List<CourseDoc> coursesToDoc(List<Course> courses) {
        List<CourseDoc> courseDocs = new ArrayList<>();
        for(Course course:courses){
            courseDocs.add(instanceDoc(course));
        }
        return courseDocs;
    }

    private CourseDoc instanceDoc(Course course) {
        CourseDoc courseDoc = new CourseDoc();
        BeanUtils.copyProperties(course,courseDoc);
        courseDoc.setStartTime(System.currentTimeMillis());
        //设置其他属性
        CourseMarket courseMarket = courseMarketMapper.selectOne(new QueryWrapper<CourseMarket>().eq("courseid", course.getId()));
        //忽略courseMarket的id
        if(courseMarket!=null){
            BeanUtils.copyProperties(courseMarket,courseDoc,"id");
        }
        String courseName=course.getName();
        String courseTypeName=courseTypeMapper.selectById(course.getCourseTypeId()).getName();
        String  tenAntName=course.getTenantName();
        courseDoc.setAll(courseName+" "+courseTypeName+" "+tenAntName);
        return courseDoc;
    }

    /**
     * 新增
     * @param courseVo
     */
    @Override
    @Transactional
    public void save(CourseVo courseVo) {

        //课程基本信息表中添加数据，返回生成的主键
        Course course = new Course();
        BeanUtils.copyProperties(courseVo,course);
        //初始化其他数据
        //模拟租户与创建人
        course.setTenantId(1L);
        course.setTenantName("金辉科技");
        course.setUserId(1L);
        course.setUserName("金辉");
        //添加数据
        baseMapper.insert(course);

        //课程详情表中添加数据
        CourseDetail courseDetail = new CourseDetail();
        courseDetail.setCourseId(course.getId());
        courseDetail.setDescription(courseVo.getDescription());
        courseDetail.setIntro(courseVo.getIntro());
        courseDetailMapper.insert(courseDetail);
    }

}
